package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.CloudGoodsMapper;
import com.bsh.mybatis.dataView.CloudGoodsInfo;
import com.bsh.mybatis.model.CloudGoods;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 积分商品
 * @author gsh
 *
 */
@Repository
public class DaoCloudGoods {
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	
	CloudGoodsMapper getMapper() {
		return session.getMapper(CloudGoodsMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(CloudGoods record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(CloudGoods record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public CloudGoods selectByPrimaryKey(Integer id) {
		CloudGoods row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(CloudGoods record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(CloudGoods record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}
	
	/**
	 * 提现分页查询
	 */
	@SuppressWarnings("deprecation")
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select g.*,m.name merchant_name from cloud_goods g ");
		sb.append("left join merchant m on m.id=g.merchant_id where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" order by id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}
	
	/**
	 * 更新更新商品
	 * @param record
	 * @return
	 */
	public int updateGoods(CloudGoods record) {
		int res = getMapper().updateByPrimaryKeyWithBLOBs(record);
		return res;
	}
	
	/**
	 * 更新商品，但不更新图片
	 * @param record
	 * @return
	 */
	public int updateGoodsExcludeImg(CloudGoods record) {
		int res = getMapper().updateGoodsExcludeImg(record);
		return res;
	}	
	
	/**
	 * 查询商品信息
	 * @param id
	 * @return
	 */
	public CloudGoodsInfo getGoods(int id){
		return getMapper().getGoods(id);
	}
	
	/**
	 * 上下架
	 * @param userid
	 * @return
	 */
	public Integer enable(int id,int isEnabele){
		Integer res = getMapper().enable(id, isEnabele);
		return res;
	}	
	
	/**
	 * 商品列表查询
	 * @param pager
	 * @param where
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public List<Map<String, Object>> getGoodsPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select name,img_small,price from cloud_goods");
		sb.append(" where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" order by id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}
	
	
}
